//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.customer;

import java.awt.event.*;
import javax.swing.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.MEUtility;

// Referenced classes of package org.jpgolay.me.customer:
//            CustomerCRUD, CustomerAdvanceSearchGUI, Customer
public class CustomerPanelController {

    private class MouseClickList extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            actionAddressView();
        }

        MouseClickList() {
        }
    }

    public CustomerPanelController(SearchBox deliverySearch, TableViewer deliveryTable, JDesktopPane desktop) {
        customerSearch = null;
        customerTable = null;
        customerSearchGui = null;
        calendarPeriod = null;
        dateComboBox = null;
        customerSearch = deliverySearch;
        customerTable = deliveryTable;
        this.desktop = desktop;
        date = new MEDate();
        crud = new CustomerCRUD();
        initSearch();
        initTable();
    }

    private void initSearch() {
        customerSearch.setList(new String[]{
                    "Code", "Raison social", "Ville", "Commercial (nom)", "Commercial (code)", "Famille"
                });
        customerSearch.setVal("");
        customerSearch.setAdvanceSearch(getCustomerSearchGUI());
        customerSearch.getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        customerSearch.getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
    }

    private void initTable() {
        customerTable.setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                });
        customerTable.setLastColumnNotEditable(0);
        customerTable.setCellEditable(false);
        customerTable.setAlternateColor(MEUtility.CUSTOMER_LINE);
        customerTable.setColumnNames(new String[]{
                    "CODE", "CLIENT", "VILLE", "FAMILLE", "COMMERCIAL"
                });
        customerTable.setPreferredColumnSize(new int[]{
                    40, 50, 40, 50, 30
                });
        actionSearch();
    }

    private CustomerAdvanceSearchGUI getCustomerSearchGUI() {
        if (customerSearchGui == null) {
            customerSearchGui = new CustomerAdvanceSearchGUI();
            customerSearchGui.getAddressViewerPanel().clean();
            customerSearchGui.getAddressViewerPanel().setCompagnyLabel("Aucune coordonn\351es");
        }
        return customerSearchGui;
    }

    private void actionSearch() {
        StringBuffer sql = new StringBuffer();
        if (customerSearch.getSelectedIndex() == 3) {
            sql.append("SELECT clients.id, raison_sociale, ville_fact, familles_clients_id, nom FROM clients, commerciaux");
        } else {
            sql.append("SELECT id, raison_sociale, ville_fact, familles_clients_id, commerciaux_id FROM clients");
        }
        switch (customerSearch.getSelectedIndex()) {
            case 0: // '\0'
                if (customerSearch.getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY id");
                }
                break;

            case 1: // '\001'
                if (customerSearch.getVal().equals("")) {
                    sql.append(" ORDER BY raison_sociale");
                } else {
                    sql.append(" WHERE raison_sociale LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY raison_sociale");
                }
                break;

            case 2: // '\002'
                if (customerSearch.getVal().equals("")) {
                    sql.append(" ORDER BY ville_fact");
                } else {
                    sql.append(" WHERE ville_fact LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY ville_fact");
                }
                break;

            case 3: // '\003'
                if (customerSearch.getVal().equals("")) {
                    sql.append(" WHERE commerciaux_id=commerciaux.id");
                    sql.append(" ORDER BY nom");
                } else {
                    sql.append(" WHERE commerciaux_id=commerciaux.id");
                    sql.append(" AND nom LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY nom");
                }
                break;

            case 4: // '\004'
                if (customerSearch.getVal().equals("")) {
                    sql.append(" ORDER BY commerciaux_id");
                } else {
                    sql.append(" WHERE commerciaux_id LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY commerciaux_id");
                }
                break;

            case 5: // '\005'
                if (customerSearch.getVal().equals("")) {
                    sql.append(" ORDER BY familles_clients_id");
                } else {
                    sql.append(" WHERE familles_clients_id LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY familles_clients_id");
                }
                break;

            default:
                if (customerSearch.getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id LIKE '");
                    sql.append(customerSearch.getVal());
                    sql.append("%' ORDER BY id");
                }
                break;
        }
        Object data[][] = crud.getDataList(sql.toString());
        if (data != null) {
            customerTable.setData(data);
            customerTable.getTable().addMouseListener(new MouseClickList());
        } else {
            customerTable.setData(getEmptyTable());
        }
    }

    private Object[][] getEmptyTable() {
        Object data[][] = new Object[3][5];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUN";
                data[i][2] = "CLIENT";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
            data[i][4] = null;
        }

        return data;
    }

    private void actionAddressView() {
        Customer customer = crud.getCustomerByPK((String) customerTable.getTable().getValueAt(customerTable.getTable().getSelectedRow(), 0));
        if (customer != null) {
            getCustomerSearchGUI().getAddressViewerPanel().setAddrLn1Label(customer.getAdresseFactUn());
            getCustomerSearchGUI().getAddressViewerPanel().setAddrLn2Label(customer.getAdresseFactDeux());
            getCustomerSearchGUI().getAddressViewerPanel().setCityLabel(customer.getVilleFact());
            getCustomerSearchGUI().getAddressViewerPanel().setCompagnyLabel(customer.getRaisonSociale());
            getCustomerSearchGUI().getAddressViewerPanel().setZipLabel(customer.getCpFact());
            getCustomerSearchGUI().getAddressViewerPanel().setStatusLabel(customer.getCivilite());
            if (customer.getNomPrenomContact() != null && !customer.getNomPrenomContact().equals("")) {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo1Label("Contact : " + customer.getNomPrenomContact());
            } else {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo1Label("");
            }
            if (customer.getTelephone() != null && !customer.getTelephone().equals("")) {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo2Label("T\351l : " + customer.getTelephone());
            } else {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo2Label("");
            }
            if (customer.getFax() != null && !customer.getFax().equals("")) {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo4Label("Fax : " + customer.getFax());
            } else {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo4Label("");
            }
            if (customer.getPortable() != null && !customer.getPortable().equals("")) {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo3Label("Port : " + customer.getPortable());
            } else {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo3Label("");
            }
            if (customer.getEmail() != null && !customer.getEmail().equals("")) {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo5Label(customer.getEmail());
            } else {
                getCustomerSearchGUI().getAddressViewerPanel().setInfo5Label("");
            }
        }
    }
    private SearchBox customerSearch;
    private TableViewer customerTable;
    private CustomerAdvanceSearchGUI customerSearchGui;
    private CalendarPeriod calendarPeriod;
    private CustomerCRUD crud;
    private JDesktopPane desktop;
    private DateComboBox dateComboBox;
    private MEDate date;
}
